import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;

public class ExecutorService_test2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        Set<Callable<String>> callables = new HashSet<>();
        callables.add(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "call one";
            }
        });

        callables.add(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "call two";
            }
        });

        callables.add(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "call three";
            }
        });

        String result = pool.invokeAny(callables);
        //invokeAny  只返回集合中某个Callable，无法保证返回的结果是哪一个，如果一个任务执行完毕或者抛出异常，方法会取消其他的Callable的执行
        System.out.println(result);

        List<Future<String>> result1 = pool.invokeAll(callables);
        for(Future<String> f:result1)
            System.out.println(f.get());

        pool.shutdown();
    }
}
